<?php
class LilShell extends Shell {
	function initialize () {
	}
	
	function main() {
		if (empty($this->args)) {
			$this->help();
		} else {
			$action = strtolower($this->args[0]);
			if (!method_exists($this, $action)) {
				$this->help();
			} else {
				unset($this->args[0]);
				call_user_func_array(array($this, $action), $this->args);
			}
		}
	}

	function heartbeat($span = 'daily') {
		if (!empty($this->args[0])) $span = $this->args[0];
		if (!in_array($span, array('daily', 'hourly', 'weekly', '5min', '10min', '15min', '30min'))) $span = 'daily';
		App::uses('LilPluginRegistry', 'Lil.Lib'); $registry = LilPluginRegistry::getInstance();
		$registry->callPluginHandlers($this, 'heartbeat_' . $span);
	}
	
	function uuid() {
		App::uses('String', 'Utility');
		$this->out(String::uuid());
	}
	
	function clear($c = 'default') {
		App::uses('Cache', 'Cache');
		$this->out(Cache::clear(false, $c));
	}
	
	function clearall() {
		App::uses('Cache', 'Cache');
		foreach (Cache::configured() as $c) {
			$this->out(Cache::clear(false, $c));
		}
	}
	
	function help() {
		$head  = "Usage: cake lil [<action>]\n";
		$head .= "---------------------------------------------------------------\n";
		$head .= "Actions:";

		$commands = array(
			'uuid' =>
				"\t'uuid' - Create new uuid.",
			'clear' =>
			 	"\t'clear [default]' - Clear target cache.",
			'clearall' =>
			 	"\t'clearall' - Clear all cache.",
	 		'heartbeat' =>
			 	"\t'heartbeat [monthly|weekly|daily|hourly|5min|10min|15min|30min]' - Execute heartbeat functionality.",
		);

		$this->out($head);
		if (!isset($this->args[1])) {
			foreach ($commands as $cmd) {
				$this->out("{$cmd}");
			}
		}
	}
}